﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Ants;

namespace MonBot.MyBinaryTree
{
    class TreeNode
    {
        public TreeNode Root, LeftChild, RightChild;
        public Direction direction;
        public int Dimension;

        public TreeNode(TreeNode root)
        {
            if (root != null)
            {
                Root = root;
            }
            else 
                Root = null;


            Dimension = 0;
        }

        public void SetValue(Direction dir)
        {
            direction = dir;
        }

        public void AttachChild(TreeNode left, TreeNode right)
        {
            if (left != null)
                LeftChild = left;
            if (right != null)
                RightChild = right;
        }

        public void RemoveChild(bool left, bool right)
        {
            if (left)
                LeftChild = null;
            if (right)
                RightChild = null;
        }


        public int UpdateDimension()
        {
            if(LeftChild == null && RightChild == null)
                return 0;



            // init
            int left  = 0;
            int right = 0;


            if(LeftChild != null)
                left  = RightChild.UpdateDimension();
            if(RightChild != null)
                right = LeftChild.UpdateDimension();


            if (left < right)
                Dimension = right +1;
            else           
                Dimension = left + 1;            
            
            return Dimension;
        }
    }
}
